'use strict'
import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../components/Login";
import Home from "../components/Home";
import Welcome from "../components/Welcome";
import Users from "../components/user/Users";
import Rights from "../components/power/Rights";
import Roles from "../components/power/Roles";
import Cate from "../components/goods/Cate";
import Params from "../components/goods/Params";
import List from "../components/goods/List";
import Add from "../components/goods/Add";
import Order from "../components/order/Order";
import Report from "../components/report/Report";



Vue.use(VueRouter)

const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            // 首页重定向
            path: '/',
            redirect: '/home'
        },
        {
            // 登录
            path: '/login',
            component: Login
        },
        {
            // 主页
            path: '/home',
            component: Home,
            redirect: '/welcome',
            children: [
                {
                    // 内容页
                    path: '/welcome',
                    component: Welcome
                },
                {
                    // 用户列表
                    path: '/users',
                    component: Users
                },
                {
                    // 权限列表
                    path: '/rights',
                    component: Rights
                },
                {
                    // 角色列表
                    path: '/roles',
                    component: Roles
                },
                {
                    // 商品分类
                    path: '/categories',
                    component: Cate
                },
                {
                    // 参数管理
                    path: '/params',
                    component: Params
                },
                {
                    // 商品分类
                    path: '/goods',
                    component: List
                },
                {
                    // 添加商品
                    path: '/goods/add',
                    component: Add
                },
                {
                    // 订单列表
                    path: '/orders',
                    component: Order
                },
                {
                    // 数据报表
                    path: '/reports',
                    component: Report
                }
            ]
        }
    ]
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // to 将要访问的路径
    // from 代表从哪个路径跳转来的
    // next next()放行 next('/xxx') 强制跳转到xxx

    if (to.path === '/login') {
        return next()
    }
    // 获取 token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) {
        return next('/login')
    }
    next()
})

export default router